//
//  RegisterViewController.m
//  Lovely Food
//
//  Created by 李经纬 on 15/11/30.
//  Copyright © 2015年 李经纬. All rights reserved.
//

#import "RegisterViewController.h"
#import "AVCloud.h"
#import "AVUser.h"
#import "LoginHelper.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UIView *myView;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *secondPassWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UILabel *noEmptyUser;
@property (weak, nonatomic) IBOutlet UILabel *noEmptyPassWord;
@property (weak, nonatomic) IBOutlet UILabel *noSamePassWord;
@property (weak, nonatomic) IBOutlet UILabel *noEmptyEmail;
@property (weak, nonatomic) IBOutlet UILabel *formatEmail;





@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加图标
    UIImageView *userImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yonghuming"]];
    userImageView.frame = CGRectMake(0, 0, 20, 20);
    self.userNameTextField.leftView = userImageView;
    self.userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    
    UIImageView *passImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mima"]];
    passImageView.frame = CGRectMake(0, 0, 20, 20);
    self.passWordTextField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mima"]];
    self.passWordTextField.leftViewMode = UITextFieldViewModeAlways;
    self.passWordTextField.leftView.frame = CGRectMake(0, 0, 20, 20);
    self.secondPassWordTextField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mima"]];
    self.secondPassWordTextField.leftView.frame = CGRectMake(0, 0, 20, 20);
    self.secondPassWordTextField.leftViewMode = UITextFieldViewModeAlways;
    
    self.emailTextField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"youxiang"]];
    self.emailTextField.leftView.frame = CGRectMake(0, 0, 20, 20);
    self.emailTextField.leftViewMode = UITextFieldViewModeAlways;
    
    self.phoneTextField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shoujihao"]];
    self.phoneTextField.leftView.frame = CGRectMake(0, 0, 20, 20);
    self.phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    
    
    
    // 观察键盘的弹出和消失
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
    
}

#pragma mark 键盘弹出
- (void)keyboardShow:(NSNotification *)notification
{
//    NSDictionary *info = notification.userInfo;
//    NSLog(@"%@",info);
    
//    float height = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
//    NSLog(@"%@~~~~~~~~%f", info, height);
    
    
    if ([self.emailTextField isFirstResponder]) {
        self.myView.frame = CGRectMake(0, - 50, self.view.frame.size.width, self.view.frame.size.height);
 
    }
    
    if ([self.phoneTextField isFirstResponder]) {
        self.myView.frame = CGRectMake(0, - 150, self.view.frame.size.width, self.view.frame.size.height);
    }
    
    
    
}

#pragma mark 键盘消失
- (void)keyboardHide:(NSNotification *)notification
{
    self.myView.frame = self.view.frame;
}
// 点击取消键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

// 处理事件
- (IBAction)handleAction:(UIButton *)sender {
    // 判断各种条件
    self.noEmptyEmail.hidden = YES;
    self.noEmptyPassWord.hidden = YES;
    self.noEmptyUser.hidden = YES;
    self.noSamePassWord.hidden = YES;
    self.formatEmail.hidden = YES;
    
    if (sender.tag == 1000) {
         [self.view endEditing:YES];
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
       
    } else if (sender.tag == 1001) {
        
        NSInteger number = 0;
        
        if (self.userNameTextField.text.length == 0 ) {
            
            self.noEmptyUser.hidden = NO;
            
            number = 1;
            
        }
        if (![self.passWordTextField.text isEqualToString:self.secondPassWordTextField.text]) {
            
            self.noSamePassWord.hidden = NO;
            
            number = 1;
            
        }
        
        if (self.passWordTextField.text.length == 0) {
            
            self.noEmptyPassWord.hidden = NO;
            
            number = 1;
            
        }
        
        if (self.emailTextField.text.length == 0) {
            
            self.noEmptyEmail.hidden = NO;
            
            number = 1;
            
        }
        
        if (number != 1) {
            
           // 注册用户并登陆
            __weak typeof (self)weakSelf = self;
            LoginHelper *login  = [[LoginHelper alloc] init];
            login.passChange = ^{
                weakSelf.formatEmail.hidden = NO;
               
            };

            [login registeWithUserName:self.userNameTextField.text passWord:self.passWordTextField.text email:self.emailTextField.text phone:self.phoneTextField.text];
            
            [self.view endEditing:YES];
        }
        
        
        
        
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
